package com.xxxt.zpc.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.xxxt.dao.CompanyMapper;
import com.xxxt.entity.Company;
import com.xxxt.zpc.service.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	CompanyMapper companyMapper;

	@Override
	public Company queryCompanyByCompanyId(String companyId) {

		return companyMapper.queryCompanyByCompanyId(companyId);
	}

	@Override
	public List<Company> selectComanyList() {

		return companyMapper.selectCompanyList();
	}

	@Override
	public boolean insertCompany(Company company) {
		if (company.getCompanyId().equals("") || company.getName().equals("")) {
			return false;
		}
		return companyMapper.insertCompany(company);
	}

	@Override
	public boolean insertCompanyList(File file) {
		/*FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		Workbook work = null;
		String fileType = file.getName().substring(file.getName().lastIndexOf("."));
		try {
			if (".xls".equals(fileType)) {
				work = new HSSFWorkbook(fis); // 2003-
			} else if (".xlsx".equals(fileType)) {
				work = new XSSFWorkbook(fis); // 2007+
			} else {

				return false;
			}
			// 1
		} catch (Exception e) {
			e.printStackTrace();

			return false;
		}
		// 2
		Sheet sheet = null;
		for (int i = 0; i < work.getNumberOfSheets(); i++) {
			sheet = work.getSheetAt(i);
			if (sheet == null)
				continue;
			Row row;
			for (int j = 1; j < sheet.getLastRowNum(); j++) {
				row = sheet.getRow(j);
				if (row.getFirstCellNum() > 1) {

				}
			}
		}*/
		return false;
	}

	@Override
	public boolean updateCompany(Company company) {
		try {
			if (company.getCompanyId() != null && !company.getCompanyId().equals("")) {
				if (company.getImg() != null &&! company.getImg().equals("")) {
					String[] imgs=selectCompanyImgById(company.getCompanyId());//从数据库获取图片
					String img = deleteCompanyImg(imgs,company.getImg());//返回新的图片
					company.setImg(img);
					return companyMapper.updateCompanyAndImg(company);
				}
				return companyMapper.updateCompany(company);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Company queryCompanyByCompanyName(String name) {
		if (name != null && !name.equals("")) {
			return companyMapper.queryCompanyByCompanyName(name);
		}
		return null;
	}

	/*
	 * 删除公司img
	 */

	public static String deleteCompanyImg(String[] imgs,String imga) {
		String[] imgArray = imga.split(",");
		
		String newImg = null;
		for (int j = 0; j < imgs.length; j++) {
			for (int i = 0; i < imgArray.length; i++) {
				if (imgs[j].equals(imgArray[i])) {
					imgs[j]=null;
					break;
				}
				
			}
		}
		for(int j = 0; j < imgs.length; j++){
			if(imgs[j]!=null){
				if(newImg!=null){
					newImg=newImg+","+imgs[j];
				}else {
					newImg=imgs[j];
				}
			}
		}
		return newImg;
	}
	/*
	 * 根据公司id获取公司图片
	 */

	@Override
	public String[] selectCompanyImgById(String id) {
		String[] img = null;
		String imgs=companyMapper.selectCompanyImgById(id);
		if(imgs==null||imgs.equals("")){
			return null;
		}
		img =imgs .split(",");
		return img;
	}

	/*
	 * 图片上传
	 * 
	 */
	@Override
	public int upload(MultipartFile[] files, String companyId, HttpServletRequest request) {
		// 先判断类型是不是图片或者是不是空
		for (MultipartFile file : files) {
			String fileName = file.getOriginalFilename();
			String type = fileName.indexOf(".") != -1
					? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
			if (type != null) {
				if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase())
						|| "JPG".equals(type.toUpperCase())) {

				} else {
					return 1;
				}
			} 
		}
		String img = companyMapper.selectCompanyImgById(companyId);
		for (MultipartFile file : files) {
			String path = null;// 文件路径
			String type = null;// 文件类型
			
			String fileName = file.getOriginalFilename();// 文件原名称
			type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()): null;
			if(type==null){
				break;
			}
			String realPath = request.getSession().getServletContext().getRealPath("/company/img/"); // 自定义的文件名称
			String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName; // 设置存放图片文件的路径
			path = realPath + trueFileName;
			File newFile=new File(path);
			// 如果文件目录不存在，创建目录
			if (!newFile.getParentFile().exists()) {
				newFile.getParentFile().mkdirs();
			}
			try {
				file.transferTo(newFile);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(img!=null&&!img.equals("")){
				img+=","+trueFileName;
			}else{
				img=trueFileName;
			}
			
			companyMapper.updateCompanyImg(img, companyId);
		}
		
		return 0;
	}

	
	//置空
	@Override
	public void aaa() {
		companyMapper.aaaa();
	}

}
